using Cysharp.Threading.Tasks;
using TEngine;

namespace GameLogic
{
    public class ProcedureOnlineLobby : ProcedureBase
    {
        protected override void OnEnter(IFsm<IProcedureManager> procedureOwner)
        {
            base.OnEnter(procedureOwner);
            AddEvent(GameEventType.OnOnlineGameStartClicked, async () =>
            {
                await UniTask.Yield();
                ChangeState<ProcedureGaming>(procedureOwner);
            });
            AddEvent(GameEventType.OnJointedLobby, () => {
                //GameModule.UI.ShowUI<ShipConfigUI>();
            });
            AddEvent<int>(GameEventType.OnDriveShipChanged,OnDriveShipChanged);
            AddEvent(GameEventType.OnBackClicked, OnBackClicked);
            AddEvent(GameEventType.OnShipConfigBackClicked, OnShipConfigBackClicked);
            GameModule.UI.ShowUI<OnlineLobbyUI>();

            UnityEngine.Debug.Log("Start,,,,,,,,,,,,,,,,,,");
        }

        protected override void OnUpdate(IFsm<IProcedureManager> procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
        }
        protected override void OnLeave(IFsm<IProcedureManager> procedureOwner, bool isShutdown)
        {
            base.OnLeave(procedureOwner, isShutdown);
            GameModule.UI.CloseUI<OnlineLobbyUI>();

        }

        protected void OnShipConfigBackClicked()
        {
            OnDriveShipChanged(0);
        }

        public void OnDriveShipChanged(int idx)
        {
            var driveShipData = GameDataModule.Instance.GetDriveShipData();
            FightLogicManager.Instance.OnShipDriverChanged();
        }

        protected void OnBackClicked()
        {
            FightLogicManager.Instance.DisconnectClient();
            ShipModule.Instance.Clear();
            ChangeState<ProcedureMain>(m_ProcedureOwner);
            FightLogicManager.Instance.Clear();
        }
    }
}
